package com.javaforever.dictionary.serviceimpl;

import com.javaforever.dictionary.dao.DicGroupDao;
import com.javaforever.dictionary.daoimpl.DicGroupDaoImpl;
import com.javaforever.dictionary.database.DBConf;
import com.javaforever.dictionary.domain.DicGroup;
import com.javaforever.dictionary.service.DicGroupService;
import java.sql.Connection;
import java.util.List;

public class DicGroupServiceImpl implements DicGroupService{
	protected DicGroupDao dao = new DicGroupDaoImpl();
	public List<DicGroup> listAllDicGroups() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listAllDicGroups(connection);
		}
	}

	public boolean updateDicGroup(DicGroup dicGroup) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.updateDicGroup(connection,dicGroup);
		}
	}

	public boolean deleteDicGroup(Long dicGroupId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.deleteDicGroup(connection,dicGroupId);
		}
	}

	public boolean addDicGroup(DicGroup dicGroup) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.addDicGroup(connection,dicGroup);
		}
	}

	public boolean softDeleteDicGroup(Long dicGroupId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.softDeleteDicGroup(connection,dicGroupId);
		}
	}

	public DicGroup findDicGroupByDicGroupId(Long dicGroupId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.findDicGroupByDicGroupId(connection,dicGroupId);
		}
	}

	public DicGroup findDicGroupByDicGroupName(String dicGroupName) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.findDicGroupByDicGroupName(connection,dicGroupName);
		}
	}

	public List<DicGroup> searchDicGroupsByDicGroupName(String dicGroupName) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.searchDicGroupsByDicGroupName(connection,dicGroupName);
		}
	}

	public List<DicGroup> listActiveDicGroups() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listActiveDicGroups(connection);
		}
	}

	public List<DicGroup> listAllDicGroupsByPage(int pagesize,int pagenum) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listAllDicGroupsByPage(connection,pagesize,pagenum);
		}
	}

	public void deleteAllDicGroups(String ids) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			dao.deleteAllDicGroups(connection,ids);
		}
	}

	public void softDeleteAllDicGroups(String ids) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			dao.softDeleteAllDicGroups(connection,ids);
		}
	}

	public Boolean toggleDicGroup(Long dicGroupId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.toggleDicGroup(connection,dicGroupId);
		}
	}

	public Boolean toggleOneDicGroup(Long dicGroupId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			DicGroup dicGroup = dao.findDicGroupByDicGroupId(connection,dicGroupId);
			if (dicGroup.getActive()==false) {
				dao.toggleDicGroup(connection,dicGroupId);
			}
			else {
				Long count = dao.countActiveDicGroupRecords(connection);
				if (count > 1){
					dao.toggleDicGroup(connection,dicGroupId);
				}
			}
			return true;
		}
	}

	public List<DicGroup> searchDicGroupsByFieldsByPage(DicGroup dicGroup,Long pagenum,Long pagesize) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.searchDicGroupsByFieldsByPage(connection,dicGroup,pagenum,pagesize);
		}
	}

	public int countDicGroupsPage(int pagesize) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countDicGroupsPage(connection,pagesize);
		}
	}

	@Override
	public Long countActiveDicGroupRecords() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countActiveDicGroupRecords(connection);
		}
	}

	@Override
	public Long countAllDicGroupRecords() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countAllDicGroupRecords(connection);
		}
	}

	@Override
	public Long countSearchDicGroupsByFieldsRecords(DicGroup dicGroup) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countSearchDicGroupsByFieldsRecords(connection,dicGroup);
		}
	}

}
